<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('资源管理列表')"/>
</head>

<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button sec:authorize="hasPermission('/sys/permission/add','sys:permission:add')"
                        class="layui-btn layui-btn-primary " id="btn-add">
                    <i class="layui-icon layui-icon-add-1"></i>
                    新增
                </button>
            </div>
            <table id="permission-table" class="layui-table" lay-filter="permission-table"></table>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<th:block th:include="include :: footer"/>
<script>
    let table;
    layui.use(['table', 'treetable'], function () {
        let $ = layui.jquery;
        table = layui.table;
        let treetable = layui.treetable;

        let prefix = pName + "/system/permission/"

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'parentId',
            elem: '#permission-table',
            url: prefix + 'list',
            page: false,
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'name', minWidth: 200, title: '权限名称'},
                    {field: 'permission', title: '权限标识'},
                    {field: 'href', title: '菜单url'},
                    {field: 'sort', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'type', width: 80, align: 'center', templet: function (d) {
                            if (d.type == 2) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            }
                            if (d.type == 1) {
                                return '<span class="layui-badge layui-bg-blue">页面</span>';
                            } else {
                                return '<span class="layui-badge-rim">目录</span>';
                            }
                        }, title: '类型'
                    },
                    {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
                ]
            ],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#permission-table');
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#permission-table');
        });
        $('#btn-fold').click(function () {
            treetable.foldAll('#permission-table');
        });
        $('#btn-add').click(function () {
            window.add();
        });

        //监听工具条
        table.on('tool(permission-table)', function (obj) {
            var layEvent = obj.event;

            if (layEvent === 'del') {
                window.remove(obj);
            } else if (layEvent === 'edit') {
                window.edit(obj);
            }
        });

        window.add = function () {
            var checkStatus = table.checkStatus('permission-table');
            let url = prefix + 'add';
            if(checkStatus.data.length > 1){
                layer.msg("最多选择一条记录");
                return;
            }else if(checkStatus.data.length == 1){
                let data = checkStatus.data[0];
                if(data.type == 2){
                    layer.msg("按钮下不允许新建")
                    return
                }
                let parentId = data.id;
                url += '?parentId=' + parentId;
                let href = data.href;
                let permission = data.permission;
                if(!!href) {
                    url += '&href=' + href
                }
                if(!!permission) {
                    url += '&permission=' + permission
                }
            }
            let layerIndex = layer.open({
                type: 2,
                title: '新增权限资源管理',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: url
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改权限资源管理',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该权限资源管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些权限资源管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('permission-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }
    });
</script>
</html>